package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.view.wheelview.WheelView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressSelectorActivity extends Activity {

    private WheelView wheelViewProvince;        //省
    private WheelView wheelViewCity;            //市
    private WheelView wheelViewCounty;          //县
    private Button btnCancel;                   //取消按钮
    private Button btnConfirm;                  //确定按钮
    private String address;

    //数据缓存
    private List<String> provinceList = new ArrayList<>();
    private Map<String, List<String>> cityMap = new HashMap<>();
    private Map<String, List<String>> areaMap = new HashMap<>();

    private String selectProvince;
    private String selectCity;
    private String selectArea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_selector);

        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        setView();
        setEvent();

    }

    private void setView() {
        wheelViewProvince = (WheelView) this.findViewById(R.id.wheelView14AddressSelector);
        wheelViewCity =  (WheelView) this.findViewById(R.id.wheelView24AddressSelector);
        wheelViewCounty =  (WheelView) this.findViewById(R.id.wheelView34AddressSelector);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4AddressSelector);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4AddressSelector);
    }

    private void setEvent() {
        btnCancel.setOnClickListener(onCancelClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);
        wheelViewProvince.setIsLoop(false);
        wheelViewCity.setIsLoop(false);
        wheelViewCounty.setIsLoop(false);

        try {
            loadData();
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> provinceArray = getProvinceList();
        wheelViewProvince.setItems(provinceArray, 0);
        selectProvince = provinceArray.get(0);
        List<String> cityArray = getCityList(selectProvince);
        wheelViewCity.setItems(cityArray, 0);
        selectCity = cityArray.get(0);
        List<String> areaArray = getAreaList(selectProvince, selectCity);
        wheelViewCounty.setItems(areaArray, 0);
        selectArea = areaArray.get(0);

        wheelViewProvince.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                selectProvince = item;
                List<String> cityArray = getCityList(selectProvince);
                wheelViewCity.setItems(cityArray, 0);
                selectCity = cityArray.get(0);
                List<String> areaArray = getAreaList(selectProvince, selectCity);
                wheelViewCounty.setItems(areaArray, 0);
                selectArea = areaArray.get(0);
            }
        });

        wheelViewCity.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                selectCity = item;
                List<String> areaArray = getAreaList(selectProvince, selectCity);
                wheelViewCounty.setItems(areaArray, 0);
                selectArea = areaArray.get(0);
            }
        });

        wheelViewCounty.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                selectArea = item;
            }
        });

    }

    //返回按钮
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    address = null;
                    AddressSelectorActivity.this.setResult(RESULT_CANCELED, null);
                    AddressSelectorActivity.this.finish();
                }
            }, 500);wheelViewProvince.setItems(getProvinceList(), 0);
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            address = selectProvince + "-" + selectCity + "-" + selectArea;
            Intent intent = new Intent();
            intent.putExtra("address", address);
            AddressSelectorActivity.this.setResult(RESULT_OK, intent);
            AddressSelectorActivity.this.finish();
        }
    };

    private void loadData() {
        String addressStr = readFromAsset("address.txt");
        Map<String, Object> addressMap = new Gson().fromJson(addressStr, Map.class);
        provinceList = (List<String>) addressMap.get("p");
        cityMap = (Map<String, List<String>>) addressMap.get("c");
        areaMap = (Map<String, List<String>>) addressMap.get("a");
    }

    //获取省份
    private List<String> getProvinceList() {
        List<String> provinces = new ArrayList<String>();
        if(provinceList != null) {
            provinces.addAll(provinceList);
        }
        return provinces;
    }

    private List<String> getCityList(String province) {
        List<String> cities = new ArrayList<String>();
        if(cityMap != null) {
            List<String> cityList = (List<String>) cityMap.get(province);
            if(cityList != null) {
                cities.addAll(cityList);
            }
        }
        return cities;
    }

    private List<String> getAreaList(String province, String city) {
        List<String> areas = new ArrayList<String>();
        if(areaMap != null) {
            String key = province + "-" + city;
            List<String> areaList = (List<String>) areaMap.get(key);
            if(areaList != null) {
                areas.addAll(areaList);
            }
        }
        return areas;
    }

    public String readFromAsset(String fileName) {
        String res = "";
        try {
            InputStream in = getResources().getAssets().open(fileName);
            int length = in.available();
            byte[] buffer = new byte[length];
            in.read(buffer);
            res = new String(buffer, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }


}
